﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using AnesSystem.Models;
using System.Windows.Media;
using Common;

namespace AnesSystem.Controls
{
    public class BaseDrugLineItemCtrl : UserControl
    {
        #region Variables
        private int _currentPage = 1;
        #endregion

        #region Constructor

        public BaseDrugLineItemCtrl()
        {
            Y1 = 0;
            Y2 = Y1;
            this.Loaded += BaseDrugLineItemCtrl_Loaded;
        }

        private void BaseDrugLineItemCtrl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        #endregion

        #region Properties
        public int CurrentPage
        {
            get
            {
                return _currentPage;
            }

            set
            {
                _currentPage = value;
            }
        }
        #endregion

        #region 依赖属性

        public double X1
        {
            get { return (double)GetValue(X1Property); }
            set { SetValue(X1Property, value); }
        }

        // Using a DependencyProperty as the backing store for X1.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty X1Property =
            DependencyProperty.Register("X1", typeof(double), typeof(BaseDrugLineItemCtrl), new PropertyMetadata(1.0));



        public virtual double X2
        {
            get { return (double)GetValue(X2Property); }
            set
            {
                SetValue(X2Property, value);
            }
        }

        // Using a DependencyProperty as the backing store for X2.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty X2Property =
            DependencyProperty.Register("X2", typeof(double), typeof(BaseDrugLineItemCtrl), new PropertyMetadata(1.0));


        public double Y1
        {
            get { return (double)GetValue(Y1Property); }
            set
            {
                SetValue(Y1Property, value);
            }
        }

        // Using a DependencyProperty as the backing store for Y1.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Y1Property =
            DependencyProperty.Register("Y1", typeof(double), typeof(BaseDrugLineItemCtrl), new PropertyMetadata(1.0));



        public double Y2
        {
            get { return (double)GetValue(Y2Property); }
            set
            {
                SetValue(Y2Property, value);
            }
        }

        // Using a DependencyProperty as the backing store for Y2.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Y2Property =
            DependencyProperty.Register("Y2", typeof(double), typeof(BaseDrugLineItemCtrl), new PropertyMetadata(1.0));



        public int MyStrokeThickness
        {
            get { return (int)GetValue(MyThicknessProperty); }
            set { SetValue(MyThicknessProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyThickness.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyThicknessProperty =
            DependencyProperty.Register("MyStrokeThickness", typeof(int), typeof(BaseDrugLineItemCtrl), new PropertyMetadata(1));




        public Brush MyStroke
        {
            get { return (Brush)GetValue(MyStrokeProperty); }
            set { SetValue(MyStrokeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyStrokeProperty =
            DependencyProperty.Register("MyStroke", typeof(Brush), typeof(BaseDrugLineItemCtrl), new PropertyMetadata(Brushes.Red));



        public PatientDrugsModel PDrugsModel
        {
            get { return (PatientDrugsModel)GetValue(PDrugsModelProperty); }
            set { SetValue(PDrugsModelProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PDrugsModel.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PDrugsModelProperty =
            DependencyProperty.Register("PDrugsModel", typeof(PatientDrugsModel), typeof(BaseDrugLineItemCtrl), new PropertyMetadata(new PatientDrugsModel()));




        public DrugSpeedModel SpeedModel
        {
            get { return (DrugSpeedModel)GetValue(SpeedModelProperty); }
            set { SetValue(SpeedModelProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SpeedModel.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SpeedModelProperty =
            DependencyProperty.Register("SpeedModel", typeof(DrugSpeedModel), typeof(BaseDrugLineItemCtrl), new PropertyMetadata(new DrugSpeedModel()));




        #endregion

        #region Event

        #endregion

        #region Private Methods

        public virtual double CaleLeftWidth(DateTime endTime, DateTime startTime)
        {
            TimeSpan tp = endTime - startTime;
            double minutes = tp.Days * 24 * 60 + tp.Hours * 60 + tp.Minutes;
            double round = (minutes / LocationUtil.SpanMinites) * LocationUtil.RealTimeItem_Width;
            return round;
        }

        public virtual void Initial(PatientDrugsModel pDrugsModel, DrugSpeedModel speedModel, DrugSpeedModel speedModel2)
        {
            if (DataManager.Instance.CurrentPatientLink == null) return;
            PDrugsModel = pDrugsModel;
            SpeedModel = speedModel;
            DateTime outRoomTime = DataManager.Instance.CurrentPatientLink.Status.OutRoomTime;
            DateTime pageStartTime = GetPageStartTime();
            if (speedModel.BeginTime.CompareTo(pageStartTime) < 0)
            {
                X1 = 0;
            }
            else
            {
                X1 = CaleLeftWidth(speedModel.BeginTime, pageStartTime);
            }

            try
            {
                if (speedModel2 != null)
                {
                    X2 = CaleLeftWidth(speedModel2.BeginTime, pageStartTime);
                }
                else
                {
                    if (outRoomTime != new DateTime())
                    {
                        X2 = CaleLeftWidth(pDrugsModel.EndTime != new DateTime() ? pDrugsModel.EndTime : outRoomTime, pageStartTime);
                    }
                    else
                    {
                        X2 = CaleLeftWidth(pDrugsModel.EndTime != new DateTime() ? pDrugsModel.EndTime : DateTime.Now, pageStartTime);
                    }
                }
                if (X1 > 768) return;
                if (X2 <= X1) X2 = X1;
                if (X2 > 768) X2 = 768;
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        public virtual void InitialPacu(PatientDrugsModel pDrugsModel, DrugSpeedModel speedModel, DrugSpeedModel speedModel2)
        {
            if (DataManager.Instance.CurrentPatientLink == null) return;
            PDrugsModel = pDrugsModel;
            SpeedModel = speedModel;
            DateTime outPacuTime = DataManager.Instance.CurrentPatientLink.Status.OutPacuTime;
            DateTime pageStartTime = GetPacuPageStartTime();
            if (speedModel.BeginTime.CompareTo(pageStartTime) < 0)
            {
                X1 = 0;
            }
            else
            {
                X1 = CaleLeftWidth(speedModel.BeginTime, pageStartTime);
            }

            try
            {
                if (speedModel2 != null)
                {
                    X2 = CaleLeftWidth(speedModel2.BeginTime, pageStartTime);
                }
                else
                {
                    if (outPacuTime != new DateTime())
                    {
                        X2 = CaleLeftWidth(pDrugsModel.EndTime != new DateTime() ? pDrugsModel.EndTime : outPacuTime, pageStartTime);
                    }
                    else
                    {
                        X2 = CaleLeftWidth(pDrugsModel.EndTime != new DateTime() ? pDrugsModel.EndTime : DateTime.Now, pageStartTime);
                    }
                }
                if (X1 > 768) return;
                if (X2 <= X1) X2 = X1;
                if (X2 > 768) X2 = 768;
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
            }
        }
        public virtual void InitialSingle(PatientDrugsModel pDrugsModel, DrugSpeedModel speedModel)
        {
            if (DataManager.Instance.CurrentPatientLink == null) return;
            PDrugsModel = pDrugsModel;
            SpeedModel = speedModel;
            DateTime pageStartTime = GetPageStartTime();
            if (speedModel.BeginTime.CompareTo(pageStartTime) < 0)
            {
                X1 = 0;
            }
            else
            {
                X1 = CaleLeftWidth(speedModel.BeginTime, pageStartTime);
            }
            if (X1 > 768) return;
        }

        public virtual void InitialPacuSingle(PatientDrugsModel pDrugsModel, DrugSpeedModel speedModel)
        {
            if (DataManager.Instance.CurrentPatientLink == null) return;
            PDrugsModel = pDrugsModel;
            SpeedModel = speedModel;
            DateTime pageStartTime = GetPacuPageStartTime();
            if (speedModel.BeginTime.CompareTo(pageStartTime) < 0)
            {
                X1 = 0;
            }
            else
            {
                X1 = CaleLeftWidth(speedModel.BeginTime, pageStartTime);
            }
            if (X1 > 768) return;
        }
        #endregion

        #region Public/Protected Methods
        public DateTime GetPageStartTime()
        {
            DateTime AnesRecordStartTime = DataManager.Instance.CurrentPatientLink.Status.AnesRecordStartTime;
            DateTime pageStartTime = AnesRecordStartTime.AddHours(4 * (CurrentPage - 1));
            return pageStartTime;
        }

        public DateTime GetPacuPageStartTime()
        {
            DateTime PacuStartTime = DataManager.Instance.CurrentPatientLink.Status.PacuStartTime;
            DateTime pageStartTime = PacuStartTime.AddHours(4 * (CurrentPage - 1));
            return pageStartTime;
        }

        public virtual void CalDrugLineX2()
        {
            if (HospitalConfig.Instance.CurrentHospital.HospitalCode.Equals(HospitalConfig.HOSPITAL_CODE_SC_CD_TYFSYY))
            {
                return;
            }
            if (DataManager.Instance.CurrentPatientLink == null) return;
            DateTime pageStartTime = GetPageStartTime();
            DateTime nowTime;
            if (DataManager.Instance.CurrentPatientLink.Status.OutRoomTime != new DateTime())
            {
                nowTime = DataManager.Instance.CurrentPatientLink.Status.OutRoomTime;
            }
            else
            {
                nowTime = DateTime.Now;
            }
            TimeSpan tp = nowTime - pageStartTime;
            double minutes = tp.Days * 24 * 60 + tp.Hours * 60 + tp.Minutes;
            double round = (minutes / LocationUtil.SpanMinites) * LocationUtil.RealTimeItem_Width;
            if (round < X1) { X2 = X1; return; }
            if (round >= 768) { X2 = 768; return; }
            X2 = round;
            if (Constants.DRUG_BENGZHU.Equals(PDrugsModel.DrugMethod))
            {
                //PDrugsModel.Yongliang = AnesRecordUtil.Instance.GetYaoLiang(PDrugsModel).ToString();
                if (HospitalConfig.Instance.CurrentHospital.HospitalCode.Equals(HospitalConfig.HOSPITAL_CODE_CQ_TLRMYY))
                {
                    PDrugsModel.Yongliang = AnesRecordUtil.Instance.GetRealYeTi(PDrugsModel).ToString();
                }
                else
                {
                    PDrugsModel.Yongliang = AnesRecordUtil.Instance.GetYaoLiang(PDrugsModel).ToString();
                }
            }
        }

        public virtual void CalPacuDrugLineX2()
        {
            if (DataManager.Instance.CurrentPatientLink == null) return;
            DateTime pageStartTime = GetPacuPageStartTime();
            DateTime nowTime;
            if (DataManager.Instance.CurrentPatientLink.Status.OutHuiFuRoomTime != new DateTime())
            {
                nowTime = DataManager.Instance.CurrentPatientLink.Status.OutRoomTime;
            }
            else
            {
                nowTime = DateTime.Now;
            }
            TimeSpan tp = nowTime - pageStartTime;
            double minutes = tp.Days * 24 * 60 + tp.Hours * 60 + tp.Minutes;
            double round = (minutes / LocationUtil.SpanMinites) * LocationUtil.RealTimeItem_Width;
            if (round < X1) { X2 = X1; return; }
            if (round >= 768) { X2 = 768; return; }
            X2 = round;
            if (Constants.DRUG_BENGZHU.Equals(PDrugsModel.DrugMethod))
            {
                if (HospitalConfig.Instance.CurrentHospital.HospitalCode.Equals(HospitalConfig.HOSPITAL_CODE_CQ_TLRMYY))
                {
                    PDrugsModel.Yongliang = AnesRecordUtil.Instance.GetRealYeTi(PDrugsModel).ToString();
                }
                else
                {
                    PDrugsModel.Yongliang = AnesRecordUtil.Instance.GetYaoLiang(PDrugsModel).ToString();

                }
            }
        }
        #endregion

        #region EventHandlers

        #endregion
    }
}